﻿package 
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class CustomInstaller extends Sprite
	{
		private const BROWSERAPI_URL_BASE: String = "http://airdownload.adobe.com/air/browserapi";

		private var _imageURL: String;
		private var _appURL: String;
		private var _appName: String;
		private var _airVersion: String;

		private var _imgLoader:Loader;
		private var _loader:Loader;
		private var _air:Object;
		
		private var _txt:TextField;
		
		public function CustomInstaller() {
			
			_txt = alert.txt;
			
			
			
			try {
				var parameters:Object = LoaderInfo(this.root.loaderInfo).parameters;


				_imageURL = validateURL(parameters["imageurl"]);

				_airVersion = String(parameters["airversion"]);

				_appURL = validateURL(encodeURI(parameters["appurl"]));

				// Make sure the appname does not contain any tags, by checking for "less than" characters
				_appName = parameters["appname"];
				if ( _appName == null || _appName.length == 0 || _appName.indexOf("<") >= 0) {
					_appName = null;

				}
			} catch (error:Error) {			
				_appURL = "";
				_appName = null;
				_airVersion = "";
			}
			
			installBtn.alpha = 0.5;
			downloadBtn.alpha = 0.5;
			
			if (_appURL!="") {
				downloadBtn.alpha = 1;
				downloadBtn.buttonMode=true;
				downloadBtn.mouseChildren=false;
				downloadBtn.addEventListener(MouseEvent.ROLL_OVER, onOver);
				downloadBtn.addEventListener(MouseEvent.ROLL_OUT, onOut);
				downloadBtn.addEventListener(MouseEvent.CLICK, onDownload);
			}
			
			
			_loader = new Loader();
			var loaderContext:LoaderContext = new LoaderContext();
			loaderContext.applicationDomain = ApplicationDomain.currentDomain;

			_loader.contentLoaderInfo.addEventListener(Event.INIT, onInit);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onAirPro);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onAirErr);
			_loader.load(new URLRequest(BROWSERAPI_URL_BASE + "/air.swf"), loaderContext);
			
			
			_txt.text = "初始化中,请稍候...";
			
			
			_imgLoader = new Loader();
			_imgLoader.load(new URLRequest(_imageURL));
			_imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgCom);
			_imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImgErr);
			
		}
		
		private function onImgCom(e:Event):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onImgCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onImgErr);
			
			_imgLoader.content.width = stage.stageWidth;
			_imgLoader.content.height = stage.stageHeight;
			
			addChildAt(_imgLoader, 0);
			
		}		
		private function onImgErr(e:IOErrorEvent):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onImgCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onImgErr);			
			
		}
		private function onAirPro(e:ProgressEvent):void 
		{
			_txt.text = "初始化中  " + int(Number(e.bytesLoaded / e.bytesTotal) * 100) + "%";
		}
		
		private function onAirErr(e:IOErrorEvent):void 
		{
			_txt.text = "初始化失败，请刷新重试。";
		}
		private function onInit(e:Event):void 
		{
			_air = e.target.content;
			switch (_air.getStatus()) {
				case "installed" :
					if (_appURL && _appURL.length > 0) {
						_txt.text = "可以选择在线安装(推荐)或下载后手动安装";
						installBtn.alpha = 1;
						installBtn.buttonMode=true;
						installBtn.mouseChildren=false;
						installBtn.addEventListener(MouseEvent.ROLL_OVER, onOver);
						installBtn.addEventListener(MouseEvent.ROLL_OUT, onOut);
						installBtn.addEventListener(MouseEvent.CLICK, onInstall);						
					} else {
						_txt.text = "不能进行在线安装，请刷新重试或下载后安装";
					}
					break;
				case "available" :
					if (_appURL && _appURL.length > 0) {
						_txt.text = "需要先安装AIR运行环境，请点击\"在线安装\"";
						installBtn.alpha = 1;
						installBtn.buttonMode=true;
						installBtn.mouseChildren=false;
						installBtn.addEventListener(MouseEvent.ROLL_OVER, onOver);
						installBtn.addEventListener(MouseEvent.ROLL_OUT, onOut);
						installBtn.addEventListener(MouseEvent.CLICK, onInstall);
						
					} else {
						_txt.text = "不能进行在线安装，请刷新重试或下载后安装";
					}
					break;
				case "unavailable" :
					_txt.text = "不能进行在线安装，请刷新重试或下载后安装";
					break;
			}
		}
		
		
		private function onOver(e:MouseEvent):void 
		{
			e.target.gotoAndStop(3);
		}
		private function onOut(e:MouseEvent):void 
		{
			e.target.gotoAndStop(1);
		}
		private function onInstall(e:MouseEvent):void 
		{
			try {
				switch (_air.getStatus()) {
					case "installed" :
						if (_appURL && _appURL.length > 0) {
							_txt.text = "开始下载文件请稍候...";
							_air.installApplication( _appURL, _airVersion );
						}
						else {
							_txt.text = "安装文件不可用，请刷新重试或下载后安装";
						}
						break;
					case "available" :
						if (_appURL && _appURL.length > 0) {
							_txt.text = "开始安装...";
							_air.installApplication( _appURL, _airVersion );
						}else {
							_txt.text = "安装文件不可用，请刷新重试或下载后安装";
						}
						break;
					case "unavailable" :
						_txt.text = "安装文件不可用，请刷新重试或下载后安装";
						break;
				}
			} catch (e:Error) {
				_txt.text = "在线不可用，请刷新重试或下载后安装";
			}
		}
		private function onDownload(e:MouseEvent):void 
		{
			navigateToURL(new URLRequest(_appURL),"_self");
		}
		
		
		
		
		
		
		
		
		
		
		
		private static function validateURL(url:String):String {
			if (url && url.length > 0) {
				var schemeMarker:int = url.indexOf(":");
				if (schemeMarker < 0) {
					schemeMarker = url.indexOf("%3a");
				}
				if (schemeMarker < 0) {
					schemeMarker = url.indexOf("%3A");
				}
				if (schemeMarker > 0) {
					var scheme:String = url.substr(0, schemeMarker).toLowerCase();
					if (scheme != "http" && scheme != "https" && scheme != "ftp") {
						url = null;
					}
				}
			}
			return url;
		}

		
		
	}
	
}